package trungnt.wipeoff;

import trungnt.wipeoff.enums.ButtonEnum;
import trungnt.wipeoff.model.GameState;
import trungnt.wipeoff.model.MyStage;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Scaling;

public class GameScreen implements ApplicationListener {

	private OrthographicCamera camera = null;
	private SpriteBatch spriteBatch = null;
	private AssetManager assetManager = null;

	private GameState oldState = null;
	private GameState currentState = null;

	private TextureRegion bgRegion = null;
	private Rectangle viewport = null;

	public GameScreen() {

	}

	@Override
	public void create() {
		// TODO Auto-generated method stub
		// Gdx.graphics.setContinuousRendering(false);
		// Gdx.graphics.requestRendering();

		this.assetManager = new AssetManager();
		this.assetManager.load("images/bg.png", Texture.class);
		this.assetManager.load("fonts/bmp/diavlo_black_72.fnt", BitmapFont.class);
		this.assetManager.finishLoading();

		this.camera = new OrthographicCamera(Cs.VW, Cs.VH);
		// this.camera.position.set(Cs.VW / 2, Cs.VH / 2, 0);
		this.camera.setToOrtho(false, Cs.VW, Cs.VH);

		this.spriteBatch = new SpriteBatch();
		this.bgRegion = new TextureRegion(this.assetManager.get("images/bg.png", Texture.class));

		this.currentState = new LoadingState(this, null);
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		Gdx.app.log("TrungNT", "game screen resize");

		Vector2 size = Scaling.fit.apply(Cs.VW, Cs.VH, width, height);

		int viewportX = (int) (width - size.x) / 2;
		int viewportY = (int) (height - size.y) / 2;
		float viewportWidth = (int) size.x;
		float viewportHeight = (int) size.y;

		this.viewport = new Rectangle(viewportX, viewportY, viewportWidth, viewportHeight);
		Gdx.gl.glViewport((int) this.viewport.x, (int) this.viewport.y, (int) this.viewport.width, (int) this.viewport.height);

		this.currentState.resize();
	}

	@Override
	public void render() {
		// TODO Auto-generated method stub
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		this.camera.update();

		this.spriteBatch.begin();
		this.spriteBatch.setProjectionMatrix(this.camera.combined);

		// draw BG
		this.spriteBatch.draw(this.bgRegion, 0, 0);

		this.spriteBatch.end();

		// Draw game(by game state)
		this.currentState.render(Gdx.graphics.getDeltaTime());
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		Gdx.app.log("TrungNT", "screen pause");
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		Gdx.app.log("TrungNT", "screen resume");

		this.currentState.resume();
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		// Gdx.app.log("TrungNT", "screen dispose");

		this.spriteBatch.dispose();

		this.assetManager.clear();
		this.assetManager.dispose();
	}

	public OrthographicCamera getCamera() {
		return this.camera;
	}

	public SpriteBatch getSpriteBatch() {
		return this.spriteBatch;
	}

	public AssetManager getAssetManager() {
		return this.assetManager;
	}

	public Rectangle getViewPort() {
		return this.viewport;
	}

	public void unloadState() {
		this.oldState.dispose();
	}

	public void changeState(GameState nextState) {
		this.oldState = this.currentState;
		this.currentState = nextState;
	}

	public void processMenuButton(ButtonEnum button) {
		switch (button) {
			case NEWGAME:
				Gdx.app.log("TrungNT", ButtonEnum.NEWGAME.toString());
				this.changeState(new PlayState(this, new MyStage()));
				Gdx.graphics.requestRendering();
				this.unloadState();
				break;
			case RANKING:
				Gdx.app.log("TrungNT", ButtonEnum.RANKING.toString());
				break;
			case HOWTOPLAY:
				Gdx.app.log("TrungNT", ButtonEnum.HOWTOPLAY.toString());
				break;
			case EXIT:
				Gdx.app.log("TrungNT", ButtonEnum.EXIT.toString());
				Gdx.app.exit();
				break;
			case MENU:
				Gdx.app.log("TrungNT", ButtonEnum.NEWGAME.toString());
				this.changeState(new MenuState(this, new MyStage()));
				Gdx.graphics.requestRendering();
				this.unloadState();
				break;
			default:
				break;
		}
	}

}
